package com.android.questions.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.WindowManager;

import com.android.base.util.ActivityClickUtil;
import com.android.base.util.DisplayUtil;
import com.android.questions.R;
import com.android.questions.config.LoginConfig;
import com.android.questions.databinding.DialogGuideBinding;
import com.android.questions.util.AnimUtil;

public class DialogGuide extends Dialog implements View.OnClickListener {

    private DialogGuideBinding guideBinding;
    private OnClickLister clickLister;
    private final Activity activity;

    public static int TYPE_LOGIN = 0;//登录成功
    public static int TYPE_TIPS = 1;//温馨提示
    private int dialogType = TYPE_LOGIN;

    public DialogGuide(Activity a) {
        super(a, R.style.dialog_loading);
        this.activity = a;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        guideBinding = DialogGuideBinding.inflate(activity.getLayoutInflater());
        setContentView(guideBinding.getRoot());

        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = DisplayUtil.getScreenWidth(activity);
        lp.height = DisplayUtil.getScreenHeight(activity);
        getWindow().setAttributes(lp);

        getWindow().setWindowAnimations(R.style.dialog_answer_correct);

        setCancelable(false);
        setCanceledOnTouchOutside(false);

        initView();
        initLister();
    }

    @SuppressLint("SetTextI18n")
    private void initView() {
        ActivityClickUtil.getInstance().clear();

        AnimUtil.receiveAnimM(activity, guideBinding.ivReceive);
        AnimUtil.rotateAnim(guideBinding.ivCenterBg);

        if (dialogType == TYPE_TIPS) {
            guideBinding.ivCenter.setImageResource(R.drawable.icon_dialog_guide_bg2);
            guideBinding.ivReceive.setImageResource(R.drawable.icon_dialog_guide_ad);
            guideBinding.textTip2.setText(Html.fromHtml("只需观看30s视频<br>就能马上<font color='#f74820'>提现</font>了"));
            guideBinding.textGiveUp.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
            guideBinding.textTip.setVisibility(View.GONE);
            guideBinding.textTip1.setVisibility(View.VISIBLE);
            guideBinding.textTip2.setVisibility(View.VISIBLE);
            guideBinding.ivTips.setVisibility(View.GONE);
            guideBinding.textGiveUp.setVisibility(View.VISIBLE);
        } else {
            guideBinding.textTip.setVisibility(View.VISIBLE);
            guideBinding.textTip1.setVisibility(View.GONE);
            guideBinding.textTip2.setVisibility(View.GONE);
            guideBinding.ivTips.setVisibility(View.VISIBLE);
            guideBinding.textGiveUp.setVisibility(View.GONE);
            guideBinding.textTip.setText(Html.fromHtml("答题就能反复<br><font color='#f74820'>赚钱提现</font>"));
//            ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) guideBinding.lottieFinger.getLayoutParams();
//            params.setMarginStart(350);
//            guideBinding.lottieFinger.setLayoutParams(params);
        }
    }

    private void initLister() {
        guideBinding.ivReceive.setOnClickListener(this);
        guideBinding.textGiveUp.setOnClickListener(this);
    }

    public DialogGuide setClickLister(OnClickLister lister) {
        this.clickLister = lister;
        return this;
    }

    public DialogGuide setType(int type) {
        this.dialogType = type;
        return this;
    }

    @Override
    public void onClick(View v) {
        if (ActivityClickUtil.getInstance().isFastClick()) {
            //防止用户快速点击
            return;
        }
        dismiss();
        if (dialogType == TYPE_TIPS) {
            LoginConfig.setGuideIndex(2);
        }

        if (this.clickLister != null) {
            if (v.getId() == R.id.textGiveUp) {
                this.clickLister.onGiveUp();
            } else {
                this.clickLister.onNext();
            }
        }
    }

    public interface OnClickLister {
        void onNext();

        void onGiveUp();
    }
}